import Joi from 'joi';
import debugCheck from '@kuiper-helpers/debug-check';
import findManyFilter from '../../common/validation/findManyFilter';
import * as validation from '../../common/validation';
import joiOmit from '../helpers/joiOmit';

export const pagination = {
	tags: ['api', 'alert'],
	description: '告警记录列表-分页查看',
	auth: debugCheck('disable-open-api-auth')
		? false
		: {
				mode: 'required',
				access: {
					// 只允许使用 URL Token 访问
					entity: 'app',
				},
		  },

	validate: {
		payload: Joi.object({
			page: Joi.number().default(1).description('当前页'),
			perPage: Joi.number().default(10).description('每页条目数'),
			filter: findManyFilter.alert
				.append({
					createTimeGTE: Joi.date().description('创建时间大于等于'),
					createTimeLTE: Joi.date().description('创建时间小于等于'),
					usernameSearch: Joi.string().description('用户名搜索'),
					targetSearch: Joi.string().description('地址搜索'),
				})
				.unknown(),
		}),
	},

	response: {
		schema: Joi.object({
			data: Joi.object({
				AlertPagination: Joi.object({
					count: Joi.number().description('总条数'),
					pageInfo: Joi.object({
						currentPage: Joi.number().description('当前页数'),
						perPage: Joi.number().description('每页条数'),
						pageCount: Joi.number().description('总页数'),
						itemCount: Joi.number().description('总条数'),
						hasNextPage: Joi.boolean().description('是否有下一页'),
						hasPreviousPage: Joi.boolean().description('是否有上一页'),
					}),
					items: Joi.array().items(
						validation.alertWithId.append({
							job: Joi.object({
								_id: Joi.string(),
								note: Joi.string().description('资产名称'),
							}),
							task: joiOmit(validation.taskWithId)
								.append({
									alertGroups: Joi.array()
										.items(joiOmit(validation.alertGroupWithId))
										.allow(null),
								})
								.allow(null), // FIXME: 需要处理没找到任务的情况
							user: Joi.object({
								_id: Joi.string(),
								username: Joi.string().description('用户名'),
							}),
						})
					),
				}),
			}),
		}).label('Response'),
	},

	handler: {
		injectGraphql: {
			payloadBuilder(request) {
				const {
					payload: { page, perPage, filter },
				} = request;

				return JSON.stringify({
					variables: { page, perPage, filter },
					// #region query
					query: `
						query($page: Int = 1, $perPage: Int = 10, $filter: FilterFindManyAlertInput, $sort: SortFindManyAlertInput = _ID_DESC) {
							AlertPagination(page: $page, perPage: $perPage, filter: $filter, sort: $sort) {
								count
								pageInfo {
									currentPage
									perPage
									pageCount
									itemCount
									hasNextPage
									hasPreviousPage
								}
								items {
									_id
									taskId
									taskName
									taskCategory
									taskType
									taskTriggerType
									target
									job {
										note
										_id
									}
									domain
									securityStatus
									title
									summary
									content
									detail
									uid
									createTime
									duration
									task {
										taskSettings {
											enable
											name
											sourceIp
											target
											collect
											alert
											interval
										}
										alertSettings {
											alertGroupIds
										}
										result {
											warnings {
												description
												impact
											}
										}
										alertGroups {
											_id
											name
										}
									}
									user {
										_id
										username
									}
								}
							}
						}`,
					// #endregion
				});
			},
		},
	},
};
